/**
 * Copyright 2015 Qiyi Inc. All rights reserved.
 *
 * @file:   Program.js
 * @path:   js-src/album/
 * @author: karkarrotxu@qiyi.com
 * @date:   2016-03-01
 */
qui(
    function (require, exports, module) {
        VODNew.Task = (function () {
            var settings = VODNew.Dao.settings;
            var dotData = VODNew.Dao.getDotData();
            var global = VODNew.Config.Global;
            var getData = function () {
                $('#addInfo,#d_addInfo').removeAttr('disabled').html('提交');

                if (settings.isOver == 0) {
                    return false;
                }
                var listUrl = dotData.listUrl;
                if (settings.taskid > 0 && settings.isStart) {
                    settings.isOver = 0
                    $.ajax({
                        type: "get",
                        url: listUrl,
                        data: {
                            'key': settings.taskid,
                            'from_client': true,
                            'index': settings.lastImg,
                            'type': 1,
                            'offset': 100
                        },
                        success: function (data) {
                            settings.isOver = 1 //防止并发
                            if (data.list.length) {
                                var li = '';
                                var sty = global.picWH;
                                var thisFpTime = "";
                                $.each(data.list, function (i, value) {
                                    var fp_time = value.fp;
                                    global.fp_pos = fp_time.lastIndexOf('/') + 1;
                                    fp_time = fp_time.substr(global.fp_pos, 10);
                                    if (!settings.isSetFpTime) {
                                        settings.firstFpTime = parseInt(fp_time);
                                        thisFpTime = '00:00:00';
                                    } else {
                                        var tmpFpTime = (parseInt(fp_time) - settings.firstFpTime);
                                        thisFpTime = VODNew.Util.getDataByNum(tmpFpTime);
                                    }
                                    //////////////////////////////////////////////////////////////////////////////////////////

                                    var s = [];
                                    s.push('<div class="text-center relative ' + thisFpTime.replace(/:/g, "-") + ' ' + value.id + '-jump" id="cate-' + value.vid + '">');
                                    s.push('<img class="fragment-img" id="img-' + value.id + '"  duration="'+value.length+'"   src="' + value.fp + '"  ondblclick="VODNew.Util.dl_img_click(event, \'' + value.id + '\');" onerror="VODCom.Player.nofind(\'' + value.fp + '\');"' + sty + ' />');
                                    s.push('<div class="absolute ' + settings.fpTime + '">' + thisFpTime + '</div><br/>')
                                    s.push('<span class="pid">' + value.id + '</span>')
                                    s.push('</div>')
                                    //////////////////////////////////////////////////////////////////////////////////
                                    $('#makedImg').append(s.join(""));
                                    //出来第一张图的时候计算几张图片一行
                                    if (!settings.isSetFpTime) {
                                        VODNew.Util.getLineWidth();
                                        settings.isSetFpTime = true;
                                    }
                                    li = value.id + 1;
                                    settings.lb++;
                                });

                                if(data.list.length > 0 ){
                                    VODCom.NewsHighlight.triggerAddNewImage();
                                }
                                if (li) {
                                    settings.lastImg = li;
                                    $('#picNum').html(li);
                                }
                                if ($('#autoRunPic').prop('checked')) {
                                    var sTop = $('#makedImg')[0].scrollHeight;
                                    $('#makedImg').scrollTop(sTop);
                                }

                            } else {
                                //请求无数据的次数
                                if (settings.noneDataNum > 5) {
                                    console.log('请求错误超过5次');
                                } else {
                                    settings.noneDataNum++
                                }
                            }
                        },
                        dataType: 'json',
                        error: function () {
                            settings.isOver = 1;
                            //alert('图片请求错误,再次尝试请求中!');
                            VODNew.Plugin.tips("error", '图片请求错误,再次尝试请求中!');
                        }
                    });
                } else {
                    console.log("null taskid")
                }
            }
            return {
                getData: getData
            }
        })()
    }
);